<!DOCTYPE html>
<html class="ui-page-login">

	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<title></title>
		<link href="../css/mui.min.css" rel="stylesheet" />
		<link href="../css/style.css" rel="stylesheet" />
		<style>
			body{
				background: #222222;
			}
			.area {
				margin: 20px auto 0px auto;
			}
			.mui-input-group:first-child {
				margin-top: 20px;
			}
			.mui-input-group label {
				width: 22%;
			}
			.mui-input-row label~input,
			.mui-input-row label~select,
			.mui-input-row label~textarea {
				width: 78%;
			}
			.mui-checkbox input[type=checkbox],
			.mui-radio input[type=radio] {
				top: 6px;
			}
			.mui-content-padded {
				margin-top: 25px;
			}
			.mui-btn {
				padding: 10px;
			}
		</style>
	</head>

	<body>
		<header class="mui-bar mui-bar-nav" style="background-color: #000000; ">
			<a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
			<h1 class="mui-title" style="color: #FFFFFF;">重置密码</h1>
		</header>
		<div class="mui-content" style="background-color: #222222;">
		<div class=" container">
				<input id='openid' type="text" class="mui-input-clear mui-input" placeholder="请输入账号">
		      	<span id='email'class="mui-input-clear mui-input" ></span>
				<button id='send'class="mui-btn">获取验证码</button>
		      	<input id='code' type="text" class="mui-input-clear mui-input" placeholder="请输入验证码">
		      	<input id='password' type="password" class="mui-input-clear mui-input" placeholder="请输入密码">
		      	<input id='confirmPassword' type="password" class="mui-input-clear mui-input" placeholder="请确认密码">
		      <button  id='reg' class="mui-btn">提交</button>
		  </div>
		</div>
		<script src="../js/mui.min.js"></script>
		<script src="../js/app.js"></script>
		<script src="../js/config.js"></script>
		<script>
			(function($, doc) {
				$.init();
				$.plusReady(function() {
					// var sendButton = doc.getElementById('sendMail');
					// var emailBox = doc.getElementById('email');
					// sendButton.addEventListener('tap', function() {
					// 	app.forgetPassword(emailBox.value, function(err, info) {
					// 		plus.nativeUI.toast(err || info);
					// 	});
					// }, false);
					var openid = document.getElementById('openid');
					var send = document.getElementById('send');
					send.setAttribute("disabled", true);   
					openid.onblur = function () {
						// console.log(openid.value);
						mui.ajax(app.getUrl() + 'userinfo/findUserEmail', {
							data: {
								userOpenId: openid.value
							},
							dataType: 'json', //服务器返回json格式数据
							type: 'post', //HTTP请求类型
							timeout: 10000, //超时时间设置为10秒；
							contentType: "application/json;charset=utf-8",
							async: false,
							beforeSend: function() {
								plus.nativeUI.showWaiting("正在获取邮箱地址...", "options");
								mask.show();
							},
							complete: function() {
								plus.nativeUI.closeWaiting();
								mask.close(); //关闭遮罩层
							},
							success: function(data) {
								 
								var email = document.getElementById('email');
								if (data.errCode === "000000") {
									send.removeAttribute("disabled");
									email.innerText = data.data; 
								} else {
									plus.nativeUI.toast('出现错误:'+data.errMsg);
									email.innerText = "该用户没有预留邮箱";
									send.setAttribute("disabled", true);
								}
							},
							error: function(xhr, type, errorThrown) {
								plus.nativeUI.toast('与服务器断开连接');
							}
						});
						
					};
					
					var timeo = 60;
					send.addEventListener('click',function(){
						var email = document.getElementById('email');
						var that = this;
						if(email.value == null || email.value == '') {
							this.setAttribute("disabled", true);
						}else {
							this.removeAttribute("disabled");
						}
						var timeStop = setInterval(function(){
						        timeo--;
						        if (timeo>0) {
						            that.innerHTML = '重新发送' + timeo +'s';
						            that.setAttribute("disabled", true);
						        }else{
						            timeo = 60;//当减到0时赋值为60
						            that.innerHTML = '获取验证码'; 
						            clearInterval(timeStop);//清除定时器
						            that.removeAttribute("disabled");
						        }
						    },1000);
						mui.ajax(app.getUrl() + 'userinfo/sendEmailCode', {
							data: {
								userOpenId: openid.value
							},
							dataType: 'json', //服务器返回json格式数据
							type: 'post', //HTTP请求类型
							timeout: 10000, //超时时间设置为10秒；
							contentType: "application/json;charset=utf-8",
							async: false,
							beforeSend: function() {
								plus.nativeUI.showWaiting("正在发送邮箱...", "options");
								mask.show();
							},
							complete: function() {
								plus.nativeUI.closeWaiting();
								mask.close(); //关闭遮罩层
							},
							success: function(data) {
								if (data.errCode === "000000") {
									plus.nativeUI.toast('发送成功，请查看邮箱！');
								} else {
									plus.nativeUI.toast('出现错误:'+data.errMsg);
								}
							},
							error: function(xhr, type, errorThrown) {
								plus.nativeUI.toast('与服务器断开连接');
							}
						});
					});
					var reg = document.getElementById('reg');
					reg.addEventListener('click',function(){
						var openid = document.getElementById('openid');
						var code = document.getElementById('code');
						var password = document.getElementById('password');
						var confirmPassword = document.getElementById('confirmPassword');
						
						var reqVo = {
							userOpenId:openid.value,
							password:password.value,
							confirmPassword:confirmPassword.value,
							code:code.value
						}
						
						
						mui.ajax(app.getUrl() + 'userinfo/updatePasswordByEmailCode', {
							data: reqVo,
							dataType: 'json', //服务器返回json格式数据
							type: 'post', //HTTP请求类型
							timeout: 10000, //超时时间设置为10秒；
							contentType: "application/json;charset=utf-8",
							async: false,
							beforeSend: function() {
								plus.nativeUI.showWaiting("正在提交...", "options");
								mask.show();
							},
							complete: function() {
								plus.nativeUI.closeWaiting();
								mask.close(); //关闭遮罩层
							},
							success: function(data) {
								if (data.errCode === "000000") {
									plus.nativeUI.toast('密码修改成功！');
									plus.webview.getLaunchWebview().show("pop-in",200,function () {
										plus.webview.currentWebview().close("none");
									});
								} else {
									plus.nativeUI.toast('出现错误:'+data.errMsg);
								}
							},
							error: function(xhr, type, errorThrown) {
								plus.nativeUI.toast('与服务器断开连接');
							}
						});
					});
					
					
				});
			}(mui, document));
		</script>
	</body>

</html>